var $ = null, lui = null, table = null;
var classifyId = null, fillingModel = null, classId = null, importId = null;
var pid, tid;

layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'upload', 'dropdown'], function () {
        lui = layui;
        var form = layui.form,
            dropdown = layui.dropdown,
            upload = layui.upload;
        $ = layui.$;
        var menuList = [
            {title: '项目级目录数据', id: 3},
            {title: '案卷级目录数据', id: 2},
            {title: '文件级目录数据', id: 1}];
        var importList = [
            {title: '按批次数据导入', id: 1},
            {title: '无批次数据导入', id: 2}];
        table = layui.table;

        pid = $("#projectList").val();
        openTaskData(table, pid);

        //模板下载
        dropdown.render({
            elem: '#downloadTemplate'
            , trigger: 'hover'
            , data: menuList
            , click: function (obj) {
                if (tid === "") {
                    layer.tips('请选择需要下载模板的工作任务。', this.elem, {tips: [1, '#5FB878']});
                    return;
                }
                if (obj.id > fillingModel) {
                    layer.tips('该任务没有设置此类型的数据。', this.elem, {tips: [1, '#5FB878']});
                    return;
                }
                window.location.href = "./getDataTemplate?pid=" + pid + "&tid=" + tid + "&model=" + obj.id;
            }
        });

        //数据导入
        dropdown.render({
            elem: '#importTemplate'
            , trigger: 'hover'
            , data: importList
            , click: function (obj) {
                top.layer.open({
                    type: 2,
                    title: '新增导入文件',
                    content: './import/setArchivesImportData?id=&pid=' + pid + "&tid=" + tid + "&importModel=" + obj.id,
                    area: ['750px', '500px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        let iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
            }
        });


        getArchivesImport({pid: pid});

        form.render();
        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTaskData(table, pid);
        });

        table.on("row(taskTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            tid = data.id;
            fillingModel = data.fillingModel;
            getArchivesImport();
        });

        function getArchivesImport(data) {
            let where = {pid: pid, tid: tid};
            if (data !== undefined) {
                where = Object.assign({}, where, data);
            }
            table.render({
                elem: "#dataTable",
                url: "./getArchivesImportData",
                contentType: "application/json",
                method: "post",
                where: where,
                cellMinWidth: 80,
                page: true,
                limit: 50,
                height: "full-65",
                loading: true,
                size: "sm",
                cols: [
                    [
                        {field: "status", title: "状态", width: 100, templet: "#statusTpl"},
                        {field: "batchCode", title: "批次号", width: 100},
                        {field: "model", title: "文件类别", width: 80, templet: "#fillingModelTpl"},
                        {field: "filename", title: "文件名", width: 340},
                        {field: "uploadTime", title: "上传时间", width: 140},
                        {field: "uploader", title: "上传人", width: 80},
                        {field: "executionTime", title: "执行时间", width: 140},
                        {field: "executor", title: "执行人", width: 80},
                        {field: "info", title: "信息"},
                        {fixed: "right", title: "操作", width: 60, toolbar: "#operationBar"},
                    ],
                ],
                done: function (res, curr, count) {
                    //表格加载完成后，默认选中第一行
                    $('.layui-table-view[lay-table-id="dataTable"]').children('.layui-table-box').children('.layui-table-body').find('table tbody tr[data-index="0"]').click();
                }
            });
        }

        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    top.layer.confirm('确定要删除当前选择的导入信息吗？ ', function (index) {
                        $.ajax({
                            url: './delArchivesImport', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: {"id": data.id},
                            success: function (result) {
                                if (result.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    OnPost = true;
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    break;
                case 'execution':
                    $.ajax({
                        url: './archivesImportExecute', //url
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: {"id": data.id},
                        success: function (result) {
                            if (result.code === 0) {
                                parent.layer.msg(result.message, {icon: 1});
                                table.reload('dataTable', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });

                            } else {
                                OnPost = true;
                                parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    break;
                case 'detail':
                    top.layer.open({
                        type: 2,
                        title: '数据导入日志',
                        content: './import/archivesImportLog?pid=' + data.id,
                        area: ['800px', '700px'],
                        shadeClose: false,
                        resize: false,

                    });
                    break;
            }
        });


    });
